/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.octopeer.core;

import java.io.File;
import java.io.StringReader;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

import org.xml.sax.InputSource;

import net.octopeer.utilities.XMLTools;
import net.octopeer.utilities.SimpleSHA1;

/**
 *
 * @author andria
 */
public class Resource {
    private String resourceID;
    private String resourceType;
    private String resourceDetail;

    public Resource() {
        //
    }
    
    public Resource(String resourceType, String resourceDetail) throws Exception {
        this.resourceType = resourceType;
        this.resourceDetail = resourceDetail;
        this.resourceID = SimpleSHA1.SHA1(this.resourceType + this.resourceDetail);
    }
    
    public void setResourceID(String resourceID) {
        this.resourceID = resourceID;
    }
    
    public void setResourceType(String resourceType) {
        this.resourceType = resourceType;
    }
    
    public void setResourceDetail(String resourceDetail) {
        this.resourceDetail = resourceDetail;
    }
    
    public String getResourceID() {
        return this.resourceID;
    }
    
    public String getResourceType() {
        return this.resourceType;
    }
    
    public String getResourceDetail() {
        return this.resourceDetail;
    }
    
    public Document toXMLDoc() throws Exception {
        Document doc;
        XMLTools xt = new XMLTools();        
        doc = xt.db.newDocument();
        
        Element rootEle = doc.createElement("Resource");

        // ID
        Element resourceIDEle = doc.createElement("Resource:ID");
        Text resourceIDText = doc.createTextNode(this.resourceID);
        resourceIDEle.appendChild(resourceIDText);
        rootEle.appendChild(resourceIDEle);        
        
        // Type
        Element resourceTypeEle = doc.createElement("Resource:Type");
        Text resourceTypeText = doc.createTextNode(this.resourceType);
        resourceTypeEle.appendChild(resourceTypeText);
        rootEle.appendChild(resourceTypeEle);        
        
        // Detail
        Element resourceDetailEle = doc.createElement("Resource:Detail");
        Text resourceDetailText = doc.createTextNode(this.resourceDetail);
        resourceDetailEle.appendChild(resourceDetailText);
        rootEle.appendChild(resourceDetailEle);        
        
        doc.appendChild(rootEle);                
        
        return doc;
    }    
    
    public String toXMLString() throws Exception {
        String result = null;
        
        XMLTools xt = new XMLTools();                
        result = xt.writeXMLString(this.toXMLDoc());
        
        return result;
    }
    
    public void toXMLFile(String fileName) throws Exception {
        // Prepare the DOM document for writing
        Source source = new DOMSource(this.toXMLDoc());

        // Prepare the output file
        File file = new File(fileName);
        Result result = new StreamResult(file);

        // Write the DOM document to the file
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.transform(source, result);
    }
    
    public Resource fromXMLDoc(Document dom) {
        Resource result = new Resource();
        
        Element rootEle = dom.getDocumentElement();
        
        if (rootEle.getTagName().equals("Resource")) {
            NodeList resourceIDList = rootEle.getElementsByTagName("Resource:ID");
            String resourceIDText = resourceIDList.item(0).getFirstChild().getNodeValue();
            NodeList resourceTypeList = rootEle.getElementsByTagName("Resource:Type");
            String resourceTypeText = resourceTypeList.item(0).getFirstChild().getNodeValue();
            NodeList resourceDetailList = rootEle.getElementsByTagName("Resource:Detail");
            String resourceDetailText = resourceDetailList.item(0).getFirstChild().getNodeValue();
            result.setResourceID(resourceIDText);
            result.setResourceType(resourceTypeText);
            result.setResourceDetail(resourceDetailText);    
        }
                
        return result;
    }
    
    public Resource fromXMLString(String data) throws Exception {
        Document dom = null;
        XMLTools xt = new XMLTools();
        Resource result = null;
        
        // Parse the XML stream input to Document type
        dom = xt.db.parse(new InputSource(new StringReader(data)));
        
        result = fromXMLDoc(dom);
        
        return result;
    }
    
    public Resource fromXMLFile(String fileName) throws Exception {
        Document dom = null;
        File file = null;
        Resource result = null;
        XMLTools xt = new XMLTools();
        
        // Parse the XML stream input to Document type
        file = new File(fileName);
        dom = xt.db.parse(file);
        
        result = fromXMLDoc(dom);
        
        return result;
    }    

//    public static void main(String[] args) throws Exception {
//        Resource r1 = new Resource("file", "./r1.res");
//        System.out.println(r1.toXMLString());
//        r1.toXMLFile("r1.xml");
//        
//        Resource r2 = new Resource();
//        r2 = r2.fromXMLFile("r1.xml");
//        r2.toXMLFile("r2.xml");
//    }
}
